package com.wu.project.admin.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.property.Constants;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xuan
 */

public enum CommonResultUtil {

    /**
     * 类实例
     */
    Instance;

    public ObjectMapper getObjectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper;
    }

    /**
     * 成功返回
     */
    public <T> JsonResult<T> success(T data) {
        return new JsonResult<T>(Constants.SUCCESS_CODE, Constants.SUCCESS_MSG, data);
    }

    public <T> JsonResult<T> success() {
        return new JsonResult<T>(Constants.SUCCESS_CODE, Constants.SUCCESS_MSG, null);
    }
    /**
     * 失败返回
     */
    public <T> JsonResult<T> error() {
        return new JsonResult<T>(Constants.ERROR_CODE, Constants.ERROR_MSG);
    }

    public <T> JsonResult<T> error(String error) {
        return new JsonResult<T>(Constants.ERROR_CODE, error);
    }

    /**
     * 失败返回
     */
    public <T> JsonResult<T> authError() {
        return new JsonResult<T>(Constants.AUTH_ERROR_CODE, Constants.AUTH_ERROR_MSG);
    }

    public Integer getOffset(Integer pageNum, Integer pageSize){
        if (pageNum <= 0){
            return 0;
        }
        return (pageNum - 1) * pageSize;
    }

    public String getNowStringTime(){
        DateTimeFormatter fmTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        return now.format(fmTime);
    }
}
